﻿using Logistics.Commn;
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using Logistics.Operators;
using Logistics.Setup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Oper
{
    //操作员管理
    public class OperatorsAppService : ApplicationService, IOperatorsAppService
    {
        private readonly IRepository<Operator, int> _RoleRepository;
        private readonly LogisticsDbContext _db;

        public OperatorsAppService(IRepository<Operator, int> RoleRepository, LogisticsDbContext db)
        {
            _RoleRepository = RoleRepository;
            _db = db;
        }

        /// <summary>
        /// 显示方法
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OperatorDto>> GetOperatorAsync(GetOperatorsListDto dto)
        {
            var oper = _db.Operator.ToList();
            var yg = _db.Basic.ToList();
            var js = _db.RoleManage.ToList();
            var list = from a in oper
                       join b in yg on a.UserId equals b.Id
                       join c in js on a.RoleId equals c.Id
                       select new OperatorDto
                       {
                           Id = a.Id,
                           SaveNumber = a.SaveNumber,
                           UserName = b.Name,
                           RoleName=c.Name,
                           DepartmentName = a.DepartmentName,
                           Phone = b.Bphone,
                           State = a.State
                       };
            if (!string.IsNullOrEmpty(dto.Numberid))
            {
                list = list.Where(m => m.SaveNumber == dto.Numberid).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Name))
            {
                list = list.Where(m => m.UserName.Contains(dto.Name)).ToList();
            }
            var totalCount = list.Count();
            list = list.Skip(dto.SkipCount).Take(dto.MaxResultCount);
            return new PagedResultDto<OperatorDto>(totalCount, list.ToList());

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperatorDto> CreateAddAsync(CreateOperatorsDto input)
        {
            var mdpass = input.PassWord.ToMd5();
            SnowflakeId snowflakeId = new SnowflakeId(1,1);
            var studentDto = new Operator
            {
                SaveNumber = snowflakeId.NextId().ToString(),
                DepartmentName = input.DepartmentName,
                UserId = input.UserId,
                RoleId = input.RoleId,
                Account =input.Account,
                PassWord = mdpass,
                DepartmentEmail = "1374527101@qq.com",
                Address= "北京海淀区",
                Fixedline= "17812089303",
                State=true,
            };
            await _RoleRepository.InsertAsync(studentDto);
            return ObjectMapper.Map<Operator, OperatorDto>(studentDto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _RoleRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="studentDto"></param>
        /// <returns></returns>
        public async Task<OperatorDto> GetAsync(int id)
        {
            var student = await _RoleRepository.GetAsync(id);
            return ObjectMapper.Map<Operator, OperatorDto>(student);
        }

        ///// <summary>
        ///// 修改
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public async Task UpdateAsync(int id, CreateOperatorsDto input)
        //{
        //    var receipts = await _RoleRepository.GetAsync(id);

        //    receipts.BasId = 1;
        //    receipts.Name = input.Name;
        //    receipts.Deptid = input.Deptid;
        //    receipts.Aliases = input.Aliases;
        //    receipts.CreateTime = DateTime.Now;

        //    await _RoleRepository.UpdateAsync(receipts);

        //}


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeletePdeleteAsync(string ids)
        {
            var a = ids.Split(',');
            foreach (var item in a)
            {
                var list = _db.Operator.Find(Convert.ToInt32(item));
                _db.Operator.RemoveRange(list);
            }

            var i = _db.SaveChanges();
            return i;
        }

        public Task UpdateAsync(int id, CreateOperatorsDto input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 员工下拉框
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<YuanGong> GetYuanGongs()
        {
            var yg = _db.Basic.ToList();
            var p = ObjectMapper.Map<List<Basic>, List<YuanGong>>(yg);
            return p;
        }

        //角色下拉框
        public List<RoleDto> GetRoleslx()
        {
            var yg = _db.RoleManage.ToList();
            var p = ObjectMapper.Map<List<RoleManage>, List<RoleDto>>(yg);
            return p;
        }
        /// <summary>
        /// 获取角色
        /// </summary>
        /// <returns></returns>
        public List<RoleDto> GetJurisdictions()
        {
            var rolelist = _db.RoleManage.ToList();
            var p = ObjectMapper.Map<List<RoleManage>, List<RoleDto>>(rolelist);
            return p;
        }

        public int PostRolePower(int id, string ids)
        {
            var p = _db.OperatorLinkRole.Where(m => m.OperatorId == id).ToList();
            if (p.Count > 0)
            {
                _db.OperatorLinkRole.RemoveRange(p);
            }

            var pid = ids.Split(',');
            foreach (var item in pid)
            {
                var r = new OperatorLinkRole
                {
                    OperatorId = id,
                    RoleId = Convert.ToInt32(item)
                };
                _db.OperatorLinkRole.Add(r);
            }
            int i = _db.SaveChanges();
            return i;
        }

        public List<OperatorLinkRoleDto> GetRolePower(int id)
        {
            var r = _db.OperatorLinkRole.Where(m => m.OperatorId == id).ToList();
            var p = ObjectMapper.Map<List<OperatorLinkRole>, List<OperatorLinkRoleDto>>(r);
            return p;
        }
    }
}
